import os, sys, json
from tqdm import tqdm

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + '../')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'riscv_build_dataset.settings')

import django

django.setup()

from obs.models import obs_package, obs_job_history
from obs import mongo_log

qs_json = []

packages = obs_package.objects.all()

MongoLog = mongo_log.OBSLog()

for package in tqdm(packages):
    build = obs_job_history.objects.filter(package=package).order_by('-build_time').first()
    if build and build.build_status_code == 'failed':
        # print(build)
        qs_json.append({
            'project': build.project.project_name,
            'package': build.package.package_name,
            'revision': build.package_revision,
            'build_time': build.build_time,
            'build_status': build.build_status_code,
            'build_duration': build.build_duration,
            'build_worker': build.build_worker,
            'log_file': f'{build.package.id}.log'
        })
        log = MongoLog.get_latest_log(build.package.id)
        with open(f'failed_log/{build.package.id}.log', 'w') as f:
            f.write(log)
            f.close()

with open("failed_builds.json", "w") as outfile:
    json.dump(qs_json, outfile, ensure_ascii=False, indent=2)
    outfile.close()
